package com.shr.lease.web.app.service.impl;

import com.shr.lease.common.context.LoginUserContext;
import com.shr.lease.common.exception.LeaseException;
import com.shr.lease.common.result.ResultCodeEnum;
import com.shr.lease.model.entity.*;
import com.shr.lease.model.enums.ItemType;
import com.shr.lease.web.app.mapper.*;
import com.shr.lease.web.app.service.RoomInfoService;
import com.shr.lease.web.app.vo.apartment.ApartmentItemVo;
import com.shr.lease.web.app.vo.attr.AttrValueVo;
import com.shr.lease.web.app.vo.fee.FeeValueVo;
import com.shr.lease.web.app.vo.graph.GraphVo;
import com.shr.lease.web.app.vo.room.RoomDetailVo;
import com.shr.lease.web.app.vo.room.RoomItemVo;
import com.shr.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Resource
    private RoomInfoMapper roomInfoMapper;
    @Resource
    private ApartmentInfoMapper apartmentInfoMapper;
    @Resource
    private LabelInfoMapper labelInfoMapper;
    @Resource
    private GraphInfoMapper graphInfoMapper;
    @Resource
    private AttrValueMapper attrValueMapper;
    @Resource
    private FacilityInfoMapper facilityInfoMapper;
    @Resource
    private PaymentTypeMapper paymentTypeMapper;
    @Resource
    private FeeValueMapper feeValueMapper;
    @Resource
    private LeaseTermMapper leaseTermMapper;
    @Resource
    private BrowsingHistoryMapper browsingHistoryMapper;


    @Override
    public IPage<RoomItemVo> pageRoomItemVoByQuery(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageRoomItemVoByQuery(page,queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long roomId) {

        //1、房间信息
        RoomInfo roomInfo = roomInfoMapper.selectRoomInfoById(roomId);
        Optional.ofNullable(roomInfo)
                .orElseThrow(() ->  new LeaseException(ResultCodeEnum.SERVICE_ERROR));
        //2、公寓信息 apartmentItemVo
        Long apartmentId = roomInfo.getApartmentId();
        //2.1、公寓基本信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectApartmentInfoById(apartmentId);
        //2.2、公寓标签信息
        List<LabelInfo> apartmentLabelInfos = labelInfoMapper.listLableInfoByApartmentId(apartmentId);
        //2.3、公寓图片信息
        List<GraphVo> apartmentGraphVos = graphInfoMapper.listGraphVo(apartmentId,ItemType.APARTMENT);
        //2.4、minRent
        BigDecimal minRent =  roomInfoMapper.getMinRent(apartmentId);
        //组装apartmentItemVo
        ApartmentItemVo apartmentItemVo = ApartmentItemVo.builder()
                .labelInfoList(apartmentLabelInfos)
                .graphVoList(apartmentGraphVos)
                .minRent(minRent)
                .build();
        BeanUtils.copyProperties(apartmentInfo,apartmentItemVo);
        //图片列表
        List<GraphVo> roomGraphVos = graphInfoMapper.listGraphVo(roomId, ItemType.ROOM);
        //属性信息
        List<AttrValueVo> roomAttrValueVos = attrValueMapper.listAttrValueVo(roomId);
        //配套信息
        List<FacilityInfo> roomFacilityInfos = facilityInfoMapper.listFacilityInfoByRoomId(roomId);
        //标签信息
        List<LabelInfo> roomLabelInfos = labelInfoMapper.listLableInfoByRoomId(roomId);
        //支付方式信息
        List<PaymentType> roomPaymentTypes = paymentTypeMapper.listByRoomId(roomId);
        //杂费信息
        List<FeeValueVo> feeValueVos = feeValueMapper.listByApartmentId(apartmentId);
        //租期信息
        List<LeaseTerm> leaseTerms = leaseTermMapper.listLeaseTermByRoomId(roomId);

        RoomDetailVo roomDetailVo = RoomDetailVo.builder()
                .apartmentItemVo(apartmentItemVo)
                .graphVoList(roomGraphVos)
                .attrValueVoList(roomAttrValueVos)
                .facilityInfoList(roomFacilityInfos)
                .labelInfoList(roomLabelInfos)
                .paymentTypeList(roomPaymentTypes)
                .leaseTermList(leaseTerms)
                .build();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        //增加浏览历史记录
        browsingHistoryMapper.insert(BrowsingHistory.builder()
                        .roomId(roomId)
                        .userId(LoginUserContext.getLoginUser().getUserId())
                        .browseTime(new Date())
                        .build());
        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> pageRoomItemVoByApartmentId(Page<RoomItemVo> page, Long apartmentId) {
        return roomInfoMapper.pageRoomItemVoByApartmentId(page,apartmentId);
    }
}




